package jllowrey.model;

import java.util.ArrayList;

/**
 * DecisionLinesEvent, an event that contains a number of Lines(choices) and rounds. The Event will allow the user
 * to set the choices, add new valid edges. It will then compute the choices prioritzation 
 *
 * @author J Lowrey
 *
 */
public class DecisionLinesEvent {

	/** the number of rounds that a user can add edges, which is the same as the number of edges a user can add **/
	public int numRounds;

	/** An arrayList of lines representing the users choices */
	public ArrayList<Line> lines = new ArrayList<Line>();

	/** the number of choices the  user can have */
	public int numChoices;

	/** the total amount of edges that can be entered*/
	int maxEdges; 

	/**
	 * DecisionLinesEvent Constructor, Adds all the lines
	 * 
	 * @param numRounds, the number of rounds in the decistionLines event
	 * 
	 */
	public DecisionLinesEvent(int numChoices, int numRounds){
		this.numRounds = numRounds;
		this.numChoices = numChoices;
		maxEdges = numChoices*numRounds;
		for (int i = 0; i < this.numChoices; i++){
			lines.add(new Line(i));
		}

	}


	/**
	 * returns a random arrayList of ranked choices, computed by following lines and edges from top to bottom.
	 * @return
	 */
//	public ArrayList<String> prioritizedChoices(){
//		return null;
//
//	}

	/**
	 * Determines if a given edge(e) is valid, returns either true or false.
	 * Uses the edges within the line to the left of the edge to compare if they are at least 7 pixels away
	 * 
	 * @param e, an edge
	 * @return
	 */
	public boolean isValidEdge(Edge e){
		
		int lineIndex;
		if(e.left - 1 < 0){
			lineIndex = 0;
		}else{
			lineIndex = e.left - 1;
		}
		
		
		for (int i = 0 ; i < lines.get(lineIndex).edges.size(); i++){
			if(Math.abs(e.height - lines.get(lineIndex).edges.get(i).height) < 7){
				return false;
			}
		}

		for (int i = 0 ; i < lines.get(e.left).edges.size(); i++){
			if(Math.abs(e.height - lines.get(e.left).edges.get(i).height) < 7){
				return false;
			}
		}
		
		for (int i = 0 ; i < lines.get(e.right).edges.size(); i++){
			if(Math.abs(e.height - lines.get(e.right).edges.get(i).height) < 7){
				return false;
			}
		}

		return true;

	}

	/**
	 * attempts to add and edge, if successful then addEdge will return true and add the edge, otherwise
	 *  it will return false and the edge will not be added
	 * 
	 * @param e, an edge
	 * @return
	 */

	public boolean addEdge(Edge e){

		if (isValidEdge(e)){
			lines.get(e.left).edges.add(e);
			return true;
		}
		else {
			System.out.println("Invalid Edge selected");
			return false;
		}
	}

	/**
	 * sets up a choice in DecisionLines Event, setting a position and name
	 * 
	 * @param position, the position of the choice in an ArrayList
	 * @param value, the name of the choice
	 * @return
	 */
	public boolean setChoice(int position, String value){
		if (!lines.contains(value)){
			lines.get(position).setChoice(value); 
			return true;
		}
		else return false;
	}

	/**
	 * Determines the completion based upon the number of edges, number of choics, and number of rounds
	 * 
	 * @return
	 */
	public boolean isDone(){

		int totalEdges = 0;

		for (int i = 0 ; i < lines.size(); i++){
			totalEdges = lines.get(i).edges.size() + totalEdges;
		}
		if (maxEdges == totalEdges){
			return true;
		}

		else return false;
	}

/** gets the num of rounds */
	public int getNumRounds() {
		return numRounds;
	}

	/** gets the lines**/
	public ArrayList<Line> getLines() {
		return lines;
	}


/**gets the number of choices*/
	public int getNumChoices() {
		return numChoices;
	}


}
